##################################
#-*- coding:utf8 -*-             #                            
from utilbs import element_a
from utilbs import bs
import utilbs

     
  
class search_result(object):
    def __init__(self):
        self.title=''
        self.href=''
        self.text=''
        
class baidu_result(search_result):
    def __init__(self,td):
        search_result.__init__(self)
        if td['class']=='f':
                a=element_a(td.a)
                text=self.get_text(td.next.nextSibling.nextSibling)
                self.text=text
                self.title=a.title
                self.href=a.href
#                print utilbs.get_text()                
#                print self
#    return {'href':url,'hreftxt':urltxt,'text':text,'cite':cite}                
    def get_text(self,text_node):
        ret=''
        br=text_node.findAll('br');
        for i in text_node.contents:
#            print type(i)
            if utilbs.is_NavigableString(i):
                ret+=i
                pass
            elif i==br[0]:
                ret=utilbs.normalize_content(ret)     
#                print ret
                return ret
            else:
                ret+=utilbs.get_text(i)    
        return
def parser(download_file,element_handle):
    baidubs=bs(download_file)
    td=baidubs.soup.findAll('td')
    for i in td:
        try:
            baidunode=baidu_result(i)
            element_handle(baidunode)
            pass
        except Exception,ex:  
#            print Exception,':',ex    
            pass  
        
        
        
test_path="d:\\threat solution\\eclipse\\workspace\\ciss\\download\\baidu.html"
baid_url='http://www.baidu.com/s?rn=100&bs=script+src+http&f=8&wd=script+src+http'
     
def test_soup():
    
    baidubs=bs(test_path)
    tds=baidubs.soup.findAll('td')
    for td in tds:
        try:
            baidunode=baidu_result(td)
            pass
        except Exception,ex:  
#            print Exception,':',ex    
            pass    
    print len(tds)
#    print soup.prettify()
#test_soup()    

        